/*
   Class Name:

      CMD2Model.

   Created by:

      Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).

   Description:

      This class will load and store a Quake 2 .md2 character model.
*/


#ifndef CMD2MODEL
#define CMD2MODEL

typedef struct
{
   int id;                             // Md2 ID number (IDP2 is the id for .md2 models).
   int version;                        // Version of the md2 (should be 8).
   int skinWidth;                      // Width of the models texture.
   int skinHeight;                     // Height of the models texture.
   int frameSize;                      // Size of frames per bytes.
   int totalSkins;                     // Number of skins the model uses.
   int numVertices;                    // Number of vertices (points) the model has.
   int numTexCoords;                   // Number of texture coordinates the model has.
   int numTriangles;                   // Number of triangles the model has.
   int numGLCommands;                  // Number of OpenGL commands the model uses.
   int numFrames;                      // Number of animation frames the model has.
   int offsetSkins;                    // Offset to the skin name (position in the file).
   int offsetTexCoords;                // Offset to where the texture coordinates are.
   int offsetTriangles;                // Offset to where the triangle data is.
   int offsetFrames;                   // Offset to where the frames start.
   int offsetGLCommands;               // Offset to where the OpenGL commands are.
   int offsetEndOfFile;                // Offset to where the file ends.
}Md2Header;


typedef struct
{
   short s;                            // First texture coordinate index.
   short t;                            // Second texture coordinate index.
}stIndex;


typedef struct
{
   unsigned short triIndex[3];         // Triangle index.
   unsigned short texCoordIndex[3];    // Texture coord index.
}stTriangleIndice;


typedef struct
{
   unsigned char vertex[3];            // Vertices of a frame.
   unsigned char normal;               // Light normal used for this frame.
}stFrameIndex;


typedef struct
{
   float scale[3];                     // Scale value used to make the model bigger.
   float translate[3];                 // Translate value for this frame.
   char name[16];                      // Frame name.
   stFrameIndex frameInfo[1];          // Index into the frame data (frame_index).
}stFrame;


class CMd2Model
{
	float currentframe;
	float oscalar;

	stTriangleIndice *TriangleList;  // List of all triangle indexs.
	stFrame *FrameList;              // List of all animation frames.
	CVector4 *vertex;                // List of models vertices.
	CVector4 *animatedMesh;          // Interpolated results of two frames.
	CVector4 *normal;                // List of models normals.
	CTexCoord *texCoords;            // List of models texture coords.

	float *ptcoords;
	float *pvertices;
	Md2Header md2Header;             // Object to hold the header in the beginning of the file.

	float geomsize;

	void MakeVBO();
	void FlushVBO();
	int rendermode;

	unsigned int IBuff;
	unsigned int VBuff;

	Shader *lerpsh;
	int scalarloc;
	int attribloc;

	unsigned int framesize;
	unsigned int tcbuffsize;

	unsigned int cnti;

	unsigned int SIB;
	unsigned int SVB;
	unsigned int STB;

   public:
      CMd2Model();
	  CMd2Model(const char *filename, Shader* modifyshader);
      ~CMd2Model();

      bool LoadModel(const char *);          // Load the .md2 model from a file.
      void AdjustTexCoords(int w, int h);// Adjust tex coords based on texture.
	  void AutoAdjustTexCoords();
      void UpdateModel(int currentFrameIndex,int nextFrameIndex,float scalar);  // Update the animation mesh.
      void Shutdown();                 // Releases all the resources the model used.

	  void Draw(int currentFrameIndex,int nextFrameIndex,float scalar);
	  int GetTotalFrames();

	  float GetSize();
	  void SetRenderMode(int mode);

	  int GetCountTris();
	  int GetCountVerts();
	  int GetCountTexCoords();
};

#endif


// Copyright August 2004
// All Rights Reserved!
// Allen Sherrod
// ProgrammingAce@UltimateGameProgramming.com
// www.UltimateGameProgramming.com